package com.million.eduservice.controller;


import com.million.commonutils.R;
import com.million.eduservice.client.VodClient;
import com.million.eduservice.entity.EduVideo;
import com.million.eduservice.entity.vo.video.VideoInfoFormVo;
import com.million.eduservice.service.EduVideoService;
import com.million.servicebase.exception.GuliException;
import com.sun.xml.bind.v2.TODO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author 十麓
 * @since 2022-03-24
 */
@Api(tags = "小节信息管理")
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;

    //添加小节信息
    @PostMapping("addVideoInfo")
    public R addVideoInfo (@RequestBody VideoInfoFormVo videoInfoFormVo) {
        eduVideoService.addVideoInfo(videoInfoFormVo);
        return R.ok();
    }

    //删除小节信息
    //TODO 后面会完善 因为还有视频，需要将小节中的视频一起删除,已完善
    @DeleteMapping("deleteVideoInfo/{videoId}")
    public R deleteVideoInfo (@PathVariable("videoId") String videoId) {

        //根据小节id  获取视频id，需要判断视频id是否为空
        EduVideo eduVideo = eduVideoService.getById(videoId);
        String videoSourceId = eduVideo.getVideoSourceId();
        if(!StringUtils.isEmpty(videoSourceId)){
            //根据视频id 删除视频
            R result = vodClient.deleteVod(videoSourceId);
            if(result.getCode() == 20001){
                throw new GuliException(20001,"删除视频失败，服务器出现异常，请稍后！");
            }
        }
        //删除小节
        eduVideoService.removeById(videoId);
        return R.ok();
    }

    //根据id查询video信息
    @GetMapping("getVideoInfo/{videoId}")
    public R getVideoInfo (@PathVariable("videoId") String videoId) {
        VideoInfoFormVo VideoInfo = eduVideoService.getVideoInfo(videoId);
        return R.ok().data("VideoInfo", VideoInfo);
    }

    //修改小节信息
    @PostMapping("updateVideoInfo")
    public R updateVideoInfo(@RequestBody VideoInfoFormVo videoInfoFormVo){
        eduVideoService.updateVideoInfo(videoInfoFormVo);
        return R.ok();
    }
}

